package com.briup.server.web.controller;

import com.briup.common.response.Result;
import com.briup.server.domain.dto.UserLoginByCodeDTO;
import com.briup.server.domain.dto.UserLoginDTO;
import com.briup.server.domain.vo.UserDetailVO;
import com.briup.server.log.LogAccess;
import com.briup.server.security.SecurityContext;
import com.briup.server.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@Tag(name = "认证接口模块")
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {

    private final UserService userService;
    @Value("${auth.token.header-name}")
    private String tokenHeaderName;


    @LogAccess("用户登录")
    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public Result<?> login(@RequestBody @Valid UserLoginDTO userLoginDTO) {
        String token = userService.login(userLoginDTO);
        return Result.success(Map.of(tokenHeaderName, token))
                .apply("登录成功");
    }

    @LogAccess("获取当前登录用户信息")
    @GetMapping("/userInfo")
    @Operation(summary = "获取当前登录用户信息")
    public Result<UserDetailVO> userInfo() {
        UserDetailVO userDetailVO = SecurityContext.currentUser();
        return Result.success(userDetailVO)
                .apply("获取用户信息成功");
    }

    @PostMapping("/loginByCode")
    @Operation(summary = "使用验证码登录")
    public Result<?> loginByCode(@RequestBody @Valid UserLoginByCodeDTO userLoginByCodeDTO) {
        String token = userService.loginByCode(userLoginByCodeDTO);
        return Result.success(Map.of(tokenHeaderName, token))
                .apply("登录成功");
    }
}
